﻿$(function(){//延迟加载
	var ysId=$('#ysdpId').val();
	var gusers=null;
	var usersbefore=new Array();
	var usersafter=new Array();
//	var cgpId=$('#cgpId').val();
	var cgpId=$('#cgpId').val();;	
	var locdpId=''; 
	$('#userlist').datagrid();
	$('#compladduserstogroup').linkbutton();
	loaduserlist(ysId);	
	function loaduserlist(departmentId){
		var toob=undefined;
//		alert(cgpId);
		if(cgpId!=''&&cgpId!=undefined){
			toob='#userbutton';
		}else{
			toob=[{
				text:'编辑人员',
				iconCls: 'icon-edit',
				handler: function(){
					var secrow=$('#userlist').datagrid('getSelected');
					parent.addTab('员工信息编辑','touserdetail?userId='+secrow.userId);					
				}
			},'-',{
				text:'删除人员',
				iconCls: 'icon-remove',
				handler: function(){
					var deleteuserIds='';
					var rows=$('#userlist').datagrid('getChecked');
					for(var i=0;i<rows.length;i++){
						deleteuserIds=deleteuserIds+rows[i].userId+',';
					}
					if(deleteuserIds!=''){
						deleteuserIds=deleteuserIds.substring(0,deleteuserIds.length-1);
					}else{
						return
					}
					$.ajax({
						url:'deleteuserIds',
						type:'POST',
						data:{
							deleteuserIds:deleteuserIds
						},
						success:function(data){
							alert(data.msg);
							loaduserlist($('#ydpId').val());
						}
					})
				}
			},'-',{
				text:'特权赋予',
				iconCls: 'icon-ok',
				handler: function(){alert('帮助按钮')}
			},'-',{
				text:'禁止权限',
				iconCls: 'icon-no',
				handler: function(){alert('帮助按钮')}
			}]
		}
		$('#userlist').datagrid({
			url:'showuserlist',
			toolbar:toob,
			queryParams:{
				departmentId:departmentId,
			},
			method:'get',
//			toolbar:toob,
//				[{
//				text:'编辑人员',
//				iconCls: 'icon-edit',
//				handler: function(){alert('编辑按钮')}
//			},'-',{
//				text:'删除人员',
//				iconCls: 'icon-remove',
//				handler: function(){alert('帮助按钮')}
//			},'-',{
//				text:'特权赋予',
//				iconCls: 'icon-ok',
//				handler: function(){alert('帮助按钮')}
//			},'-',{
//				text:'禁止权限',
//				iconCls: 'icon-no',
//				handler: function(){alert('帮助按钮')}
//			},'-',{
//				text:'测试更新',
//				iconCls: 'icon-no',
//				handler: function(){
//					var rows=$('#userlist').datagrid('getChecked');
//					for(var i=0;i<rows.length;i++){
//						alert(rows[i].userId);
//					}
//				}
//			}],
			columns:[[{
				field:'ck',
				checkbox:true
			},{
				field:'username',
				title:"员工姓名",
				width:'10%'
			},{
				field:'tel',
				title:"手机号码",
				width:'14%'
			},{
				field:'loctel',
				title:"固定电话",
				width:'14%'
			},{
				field:'dpname',
				title:"部门名称",
				width:'13%'
			},{
				field:'rootdpname',
				title:"隶属部门",
				width:'13%'
			},{
				field:'rolename',
				title:"职位",
				width:'10%'
			},{
				field:'ifleader',
				title:"是否部门负责人",
				width:'5%'
			},{
				field:'email',
				title:"邮箱",
				width:'15%'
			}]],
			fit:true,
			pagination:true,
			rownumbers:true,
			onLoadSuccess:function(data){
				cgpId=$('#cgpId').val();
//				alert('ok');
				usersbefore= new Array();//初始化默认的人员选择
				var udrows=data.rows;
				var t=0;
				$(this).datagrid('hideColumn','ck');
				if(cgpId!=''){//分组选人模式
					$(this).datagrid('showColumn','ck');
					$.ajax({
						url:'getgusers?gpId='+cgpId,
						success:function(data){
							gusers=data;
							for(var i=0;i<udrows.length;i++){//遍历所有行
								for(var j=0;j<data.length;j++){//遍历取得的组所有的用户
									if(udrows[i].userId==data[j].userId){	
										usersbefore[t]=data[j].userId;
										t++;
										$('#userlist').datagrid('checkRow',i);
										break;
									}
								}
							}
							$('#ysdpId').val(departmentId);
							$('#compladduserstogroup').linkbutton();
						}
					});
				}else{//分组ID不为空，那么正在给分组选配人员
					
				}		
			}	
		});
	}
		// get the pager of datagrid
	$('#compladduserstogroup').linkbutton({
		plain:true,
		text:'添加到分组',
		width:'220px',
		iconCls: 'icon-ok',
		onClick:function(){
			usersafter= new Array();
			var newuserIds='';
			var deleteuserIds='';
			var chrows=$('#userlist').datagrid('getChecked');
//			alert(usersbefore);
			for(var i=0;i<chrows.length;i++){//取得现在所选的ID 
				usersafter[i]=chrows[i].userId;
				var flag1=0;
				for(var j=0;j<usersbefore.length;j++){
					if(usersafter[i]==usersbefore[j]){//重复区域
						flag1=1;
//						usersbefore[j]='';
						break;
					}
				}
				if(flag1==1){//有重复区域
					
				}else{//没有重复区域，则需要新增
					newuserIds=newuserIds+usersafter[i]+',';
				}
			}			
			for(var i=0;i<usersbefore.length;i++){
				var flag1=0;
				for(var j=0;j<usersafter.length;j++){
					if(usersbefore[i]==usersafter[j]){
						flag1=1;
						break;
					}
				}
				if(flag1==1){
					
				}else{//没有重复则需删除
					deleteuserIds=deleteuserIds+usersbefore[i]+',';
				}
			}
			if(newuserIds!='')
			newuserIds=newuserIds.substring(0,newuserIds.length-1);
			if(deleteuserIds!='')
			deleteuserIds=deleteuserIds.substring(0,deleteuserIds.length-1);
			if(newuserIds!=''||deleteuserIds!=''){
				$.ajax({
					url:'addusertogroup',
					type:'POST',
					data:{
						newuserIds:newuserIds,
						deleteuserIds:deleteuserIds,
						groupId:cgpId
					},
					success:function(data){
						alert(data.msg);
//						$('#userlist').datagrid('reload',{
//							departmentId:$('#ysdpId').val()
//						});
//						alert($('#ydpId').val());
						loaduserlist($('#ydpId').val());
					}
				});
			}
//			alert('要添加的:'+newuserIds);
//			alert('要删除的:'+deleteuserIds);
		}
	});
	
	
	
	
//	$('#ceshi').linkbutton({
//		text:'测试',
//		onClick:function(){
//			$('#userlist').datagrid('reload',{
//				url:'showuserlist?departmentId='+'root',
//			});
//		}
//	});
//	if($('#ysdpId').val()==''){//如果dpId有值，则载入userlist
//		//alert($('#ysdpId').val());
//		$('#userlist').datagrid('reload',{
//			departmentId:'root'
//		});
//	}else{
//		$('#userlist').datagrid('reload',{
//			departmentId:$('#ysdpId').val()
//		});
//	}
});